package lgs.hm.controller;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import lgs.hm.model.User;

//项目/模块路径/方法路径

@Controller
@RequestMapping("/hello")
public class HelloController {
	
//	@RequestMapping(value="/{addr}/info",method=RequestMethod.GET,params= {"name","age!=10"},headers= {"User-Agent"})
	@GetMapping(value="/{addr}/info",headers= {"User-Agent"})
	@ResponseBody
	public String info(@PathVariable("addr") String abc,@RequestHeader("User-Agent") String ua) {
		return "<h1>这里是一个独立的Controller文件:"+ua+"</h1>";
	}
	
	@GetMapping("testcook")
	@ResponseBody
	public  String cook(HttpServletRequest reqeust) {
		Cookie[] cookies = reqeust.getCookies();
		String cv="";
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("aaa")) {
				cv=cookie.getValue();
			}
			
		}
		return cv;
	}
	
	//获取cookie
	@GetMapping("testcook2")
	public @ResponseBody String cook2(
			@CookieValue("aaa") String sv) {
		return sv;
	}
	
	@GetMapping("login")
	public @ResponseBody Object login(@Valid User user) {
		user.setUserName("刘德华");
		return user;
	}
	
	@GetMapping("logins")
	public String logins(User user,Map<String, Object> map) {
		//通过Map往jsp里面注入数据，等价于request.setAttribute(name,value)
		user.setUserName("刘德华");
		user.setPassword("454545");
		map.put("User", user);  //进入到request作用域，相当于request.setAttribute();
		return "users";
	}
	

}
